package com.ruoyi.framework.web.exception.base;

import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.xml.bind.annotation.XmlTransient;
import java.util.HashMap;

/**
 * 通用分页实体类
 *
 * @param <K>
 * @param <V>
 */
public class XParamMap<K, V> extends HashMap<K, V> {

    /**
     * 当前实体分页对象
     */
    private Page<Object> page;
    private int cPage;

    @JsonIgnore
    @XmlTransient
    public Page<Object> getPage() {
        if (page == null) {
            page = new Page<>();
        }
        return page;
    }

    /**
     * 设置分页实体类
     *
     * @param page 分页实体类
     * @return 返回分页实体类
     */
    public Page<Object> setPage(Page<Object> page) {
        this.page = page;
        return page;
    }

    /**
     * 初始化page分页
     *
     * @param pageSize    每页数据数量
     * @param currentPage 当前页页面
     * @return 返回分页实体类
     */
    public Page<Object> intPage(String pageSize, String currentPage) {
        this.page = new Page<>();
        try {
            int parseInt = Integer.parseInt(pageSize);
            if (parseInt > 0) {
                page.setPageSize(parseInt);
            } else {
                page.setPageSize(50);
            }
        } catch (Exception e) {
            page.setPageSize(50);
        }
        try {
            int parseInt = Integer.parseInt(currentPage);
            if (parseInt > 0) {
                cPage = parseInt;
                page.setPageNo(parseInt);
            } else {
                cPage = 1;
                page.setPageNo(1);
            }
        } catch (Exception e) {
            cPage = 1;
            page.setPageNo(1);
        }
        return page;
    }

    /**
     * 获取当前页码
     *
     * @return 当前页码
     */
    public int getCurrentPage() {
        return cPage;
    }

    /**
     * 获取总页数
     *
     * @return 总页数
     */
    public int getTotalPage() {
        try {
            return (int) (page.getCount() / page.getPageSize()) + (page.getCount() % page.getPageSize() == 0 ? 0 : 1);
        } catch (Exception e) {
            return 0;
        }
    }
}
